package cn.gongwebo.payview.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * 支付键盘
 * Created by GongWenBo on 2017/11/23.
 */

public class KeyboardView extends View {

    private static final String TAG = "KeyboardView";
    private Paint mPaint;
    private int WIDTH_LINES  = 3;        // 水平的分割线数
    private int HEIGHT_LINES = 4;        // 竖直的分割线数
    private int    mAvgWidth;
    private int    mAvgHeight;
    private Canvas mCanvas;

    public KeyboardView(Context context) {
        this(context, null);
    }

    public KeyboardView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public KeyboardView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        mCanvas = canvas;
        Log.d(TAG, "onDraw: width=" + width + "\theight=" + height);
        // 画竖线
        mPaint.setColor(Color.BLACK);
        mAvgWidth = width / WIDTH_LINES;
        for (int i = 1; i < WIDTH_LINES; i++) {
            canvas.drawLine(mAvgWidth * i, 0, mAvgWidth * i, height, mPaint);
        }
        // 画横线
        mAvgHeight = height / HEIGHT_LINES;
        for (int i = 0; i < HEIGHT_LINES; i++) {
            canvas.drawLine(0, mAvgHeight * i, width, mAvgHeight * i, mPaint);
        }
        // 画数字
        mPaint.setTextSize(40);
        Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt();
        // 画1-3
        drawNum(1, 1, "1", fontMetrics);
        drawNum(1, 2, "2", fontMetrics);
        drawNum(1, 3, "3", fontMetrics);
        // 画4-6
        drawNum(2, 1, "4", fontMetrics);
        drawNum(2, 2, "5", fontMetrics);
        drawNum(2, 3, "6", fontMetrics);
        // 画7-9
        drawNum(3, 1, "7", fontMetrics);
        drawNum(3, 2, "8", fontMetrics);
        drawNum(3, 3, "9", fontMetrics);
        // 画底部
        drawNum(4, 2, "0", fontMetrics);
        mPaint.setColor(Color.GRAY);
        //        canvas.drawRect(0, mAvgHeight * 3, mAvgWidth, mAvgHeight * 4, mPaint);
        //        canvas.drawRect(mAvgWidth * 2, mAvgHeight * 3, mAvgWidth * 3, mAvgHeight * 4, mPaint);
//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.back);
//        int bWidth = bitmap.getWidth();
//        int bHeight = bitmap.getHeight();
//        canvas.drawBitmap(bitmap, (float) (mAvgWidth * 2.5 - bWidth / 2), (float) (mAvgHeight * 3.5 - bHeight / 2), mPaint);
    }

    public void drawRect(int height, int width, boolean normal) {
        mPaint.setColor(normal ? Color.WHITE : Color.GRAY);
        mCanvas.drawRect(mAvgWidth * (width - 1), mAvgHeight * (height - 1), mAvgWidth * width, mAvgHeight * height, mPaint);
    }

    public void drawNum(int height, int width, String num, Paint.FontMetricsInt fontMetrics) {
        // 保证竖直居中
        int baseline = (mAvgHeight * (height - 1) + mAvgHeight * height - fontMetrics.bottom - fontMetrics.top) / 2;
        // 保证水平居中
        mPaint.setTextAlign(Paint.Align.CENTER);
        mCanvas.drawText(num, mAvgWidth * (width - 1) + mAvgWidth / 2, baseline, mPaint);
    }

    private float mStartX;
    private float mStartY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mStartX = event.getX();
                mStartY = event.getY();
                if (mStartX <= mAvgWidth) {
                    if (mStartY <= mAvgHeight) {
                        Log.d(TAG, "onTouchEvent: " + "1");
                    } else if (mStartY > mAvgHeight && mStartY <= mAvgHeight * 2) {
                        Log.d(TAG, "onTouchEvent: " + "4");
                    } else if (mStartY > mAvgHeight * 2 && mStartY <= mAvgHeight * 3) {
                        Log.d(TAG, "onTouchEvent: " + "7");
                    } else {
                        Log.d(TAG, "onTouchEvent: " + " ");
                    }
                } else if (mStartX > mAvgWidth && mStartX <= mAvgWidth * 2) {
                    if (mStartY <= mAvgHeight) {
                        Log.d(TAG, "onTouchEvent: " + "2");
                    } else if (mStartY > mAvgHeight && mStartY <= mAvgHeight * 2) {
                        Log.d(TAG, "onTouchEvent: " + "5");
                    } else if (mStartY > mAvgHeight * 2 && mStartY <= mAvgHeight * 3) {
                        Log.d(TAG, "onTouchEvent: " + "8");
                    } else {
                        Log.d(TAG, "onTouchEvent: " + "0");
                    }
                } else {
                    if (mStartY <= mAvgHeight) {
                        Log.d(TAG, "onTouchEvent: " + "3");
                    } else if (mStartY > mAvgHeight && mStartY <= mAvgHeight * 2) {
                        Log.d(TAG, "onTouchEvent: " + "6");
                    } else if (mStartY > mAvgHeight * 2 && mStartY <= mAvgHeight * 3) {
                        Log.d(TAG, "onTouchEvent: " + "9");
                    } else {
                        Log.d(TAG, "onTouchEvent: " + "删除");
                    }
                }
                //                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                mStartX = 0;
                mStartY = 0;
                //                invalidate();
                break;
        }
        return true;
    }


}
